package com.define.ssh.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.define.ssh.dao.DeptuserDao;
import com.define.ssh.model.Deptuser;
import com.define.ssh.util.InterfaceIpData;
import com.define.ssh.util.Pager;

@Repository("deptuserDaoImpl")
public class DeptuserDaoImpl extends BaseDaoImpl<Deptuser, Long> implements
		DeptuserDao {

	/***
	 * 删除该用户 对应的部门
	 * 
	 * @param user_id
	 */
	@Override
	public void delDeptUserByUserId(Long user_id) {
		String hql = "delete deptuser du where 1=1 ";
		hql += " and du.userid=" + user_id;
		getSession().createQuery(hql).executeUpdate();
	}

	@Override
	public Pager findPagerByAccount(Pager pager, HashMap<String, Object> hm) {
		final Criteria criteria = getSession().createCriteria(Deptuser.class);

		if (hm.get("user_type") != null || hm.get("forbidden") != null
				|| hm.get("name") != null) {
			criteria.createAlias("userInfo", "userInfo");
		}
		if (hm.get("deptids") != null) {
			// criteria.add(Restrictions.eq("deptid", hm.get("deptid")));
			
			final String ds = hm.get("deptids").toString();
			final String str[] = ds.split(",");
			final Integer array[] = new Integer[str.length];
			for (Integer i = 0; i < str.length; i++) {
				array[i] = Integer.parseInt(str[i]);
			}
			criteria.add(Restrictions.in("deptid", array));
		} else {
			criteria.add(Restrictions.eq("deptid", 0));
		}
		if (hm.get("user_type") != null
				&& StringUtils.hasText(hm.get("user_type").toString())) {
			criteria.add(Restrictions.eq("userInfo.user_type",
					Integer.valueOf(hm.get("user_type").toString())));
		}
		
		
		if (hm.get("updatetype") != null
				&& StringUtils.hasText(hm.get("updatetype").toString())) {
			 Integer array1[] = new Integer[]{1,2};
			criteria.add(Restrictions.in("updatetype", array1 ));
		}
		
		if (hm.get("forbidden") != null
				&& StringUtils.hasText(hm.get("forbidden").toString())) {
			criteria.add(Restrictions.eq("userInfo.forbidden",
					Integer.valueOf(hm.get("forbidden").toString())));
		}

		if (hm.get("name") != null
				&& StringUtils.hasText(hm.get("name").toString())
				&& !"输入姓名、账号等进行查询".equals(hm.get("name").toString())) {
//			 criteria.createAlias("userInfo", "userInfo");
//			criteria.add(Restrictions.like("userInfo.name", "%"
//					+ hm.get("name").toString() + "%"));
			 criteria.add(Restrictions.or(Restrictions.like("userInfo.name",
			 "%" + hm.get("name").toString() + "%"),
			 Restrictions.like("userInfo.userCode", "%" +
			 hm.get("name").toString() + "%")));

		}

		criteria.addOrder(Order.desc("userid"));
		// pager.setSearchBy("");
		pager = findPager(pager, criteria);
		/*
		 * pager.setSearchBy(searchBy); pager.setKeyword(keyword);
		 */
		return pager;
	}

	@Override
	public Pager findPagerByFictitiousTeamId(Long fteamId, Pager pager) {
		final Criteria criteria = getSession().createCriteria(Deptuser.class);
		criteria.add(Restrictions.sqlRestriction("userid not in (select fuser.userid from TB_Fictitious_User fuser where fuser.fid="+fteamId+" and fuser.update_Type in (1,2))"));
		criteria.createAlias("dept", "c");  
		criteria.add(Restrictions.eq("c.company.id",InterfaceIpData.compid));
		String keyword=pager.getKeyword();
		if(org.apache.commons.lang3.StringUtils.isNotEmpty(keyword)){
			criteria.createAlias("userInfo", "userInfo");
			criteria.createAlias("dept", "c");
			criteria.add(Restrictions.eq("c.deptid", 0));
			criteria.add(Restrictions.or(Restrictions.like("userInfo.userCode", "%"+keyword+"%"), Restrictions.or(Restrictions.like("userInfo.name", "%"+keyword+"%"), Restrictions.like("c.deptname", "%"+keyword+"%"))));
			pager.setKeyword(null);
		}
		pager = findPager(pager, criteria);
		pager.setKeyword(keyword);
		return pager;
	}

	@Override
	public Pager findPagerByPowerList(List<Integer> power_list, Pager pager) {
		final Criteria criteria = getSession().createCriteria(Deptuser.class);
		criteria.add(Restrictions.sqlRestriction("userid not in (select u.id from user u where power in ("+power_list.toString().replace("[", "").replace("]", "")+"))"));
		criteria.createAlias("dept", "c");  
		criteria.add(Restrictions.eq("c.company.id",InterfaceIpData.compid));
		String keyword=pager.getKeyword();
		if(org.apache.commons.lang3.StringUtils.isNotEmpty(keyword)){
			criteria.createAlias("userInfo", "userInfo");
			criteria.add(Restrictions.or(Restrictions.like("userInfo.userCode", "%"+keyword+"%"), Restrictions.or(Restrictions.like("userInfo.name", "%"+keyword+"%"), Restrictions.like("c.deptname", "%"+keyword+"%"))));
			pager.setKeyword(null);
		}
		pager = findPager(pager, criteria);
		pager.setKeyword(keyword);
		return pager;
	}
	
	@Override
	public Pager find(Pager pager, Integer deptid, String data) {
		Criteria crit = getSession().createCriteria(Deptuser.class);
		if(deptid!=null){
			crit.add(Restrictions.eq("deptid",Integer.valueOf(deptid)));
		}
		if(org.apache.commons.lang3.StringUtils.isNotEmpty(data)){
			crit.createAlias("userInfo", "userInfo");
			crit.add(Restrictions.or(Restrictions.like("userInfo.name", "%"+data+"%"), Restrictions.like("userInfo.userCode", "%"+data+"%")));
		}
		crit.add(Restrictions.ne("updatetype",3));
		return this.findPager(pager, crit);
	}
}
